package com.cyq.day11;

import javax.swing.*;
import java.time.LocalDate;

public class StudentEatingSystem {
    static int index=0;
    static int count=0;
    static int recordindex=0;
    static String[] record=new String[10];
    static String[][] ary = {
            {"张三", "111111", "1000",},
            {"李四", "222222", "2000",},
            {"王五", "333333", "3000",}
    };

    public static void main(String[] args) {
        welcome();
    }

    public static void welcome() {
        String input = JOptionPane.showInputDialog(null, "欢迎光临，XX食堂饭卡终端机\n" +
                "\n" +
                "请选择:\n" +
                "1.登录系统\n" +
                "2.退出系统");
        if (input.equals("2")) {
            JOptionPane.showMessageDialog(null, "谢谢使用，请收好卡片");
            System.exit(0);
        } else if (input.equals("1")) {
            login();
        } else {
            JOptionPane.showMessageDialog(null, "输入有误请重新输入");
            return;
        }
    }

    public static void login() {
        for (int i = 0; i < 3; i++) {
            String userName = JOptionPane.showInputDialog(null, "请输入用户名");
            String passWord = JOptionPane.showInputDialog(null, "请输入密码");
            for (int j = 0; j < ary.length; j++) {
                if (ary[j][0].equals(userName) && ary[j][1].equals(passWord)) {
                    index=j;
                    menu();
                }
            }
            JOptionPane.showMessageDialog(null, "用户名或密码错误");
        }
        JOptionPane.showMessageDialog(null, "非法用户");
        System.exit(0);
    }

    public static void menu() {
        while (true) {
            String input = JOptionPane.showInputDialog(null, "请选择:\n" +
                    "1.充值\n" +
                    "2.刷卡消费\n" +
                    "3.查询余额\n" +
                    "4.查询交易记录\n" +
                    "5.修改密码\n" +
                    "6.安全退出");
            if (input.equals("1")) {
                reCharge();
            } else if (input.equals("2")) {
                use();
            } else if (input.equals("3")) {
                showRestMoney();
            } else if (input.equals("4")) {
                showDealRecord();
            } else if (input.equals("5")) {
                changePwd();
            } else if (input.equals("6")) {
                welcome();
            } else {
                JOptionPane.showMessageDialog(null, "输入有误请重新输入");
            }
        }
    }
    public static void reCharge(){
        String input=JOptionPane.showInputDialog(null,"请输入充值金额");
        int result=Integer.parseInt(ary[index][2])+Integer.parseInt(input);
        ary[index][2]=result+"";
        record[recordindex]="充值"+input+"元";
        recordindex++;
    }
    public static void use(){
        String input=JOptionPane.showInputDialog(null,"请输入消费金额");
        if (Integer.parseInt(input)>Integer.parseInt(ary[index][2])){
            JOptionPane.showMessageDialog(null,"余额不足");
        }else{
            int result=Integer.parseInt(ary[index][2])-Integer.parseInt(input);
            ary[index][2]=result+"";
            count+=Integer.parseInt(input);
            record[recordindex]="消费"+(input)+"元";
            recordindex++;
        }
    }
    public static void showRestMoney(){
        JOptionPane.showMessageDialog(null,"现有余额:"+ary[index][2]+"\n"+
                "累计消费:"+count);
    }
    public static void showDealRecord(){
        String[] strings = new String[record.length];
        for (int i=0;i<record.length;i++){
           strings[i]=record[i]+"    "+ LocalDate.now().toString();
        }
         JOptionPane.showMessageDialog(null,"交易信息     日期\n"+strings[0]+"\n"+
                 strings[1]);
    }
    public static void changePwd(){
        String input= JOptionPane.showInputDialog(null,"请输入旧密码");
        if (ary[index][1].equals(input)){
            String newPwd =JOptionPane.showInputDialog(null,"请输入新密码");
            String newPwd1=JOptionPane.showInputDialog(null,"请确认新密码");
            if (!newPwd.equals(newPwd1)){
                JOptionPane.showMessageDialog(null,"两次密码不一致");
            }else{
                ary[index][1]=newPwd;
            }
        }else {
            JOptionPane.showMessageDialog(null,"旧密码错误");
        }
    }

}
